package com.itheima.edu.info.manger.dao;

import com.itheima.edu.info.manger.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class OtherStudentDao {
    //private static Student[] stus = new Student[5];
        private static ArrayList<Student> studentArrayList = new ArrayList<>();
    static {
        studentArrayList.add(new Student("heima001","张三","23","1999-11-11"));
        Student stu2 = new Student("heima002","李四","24","2000-11-11");


    }

    // 添加学生方法
    public boolean addStudent(Student stu) {

        return studentArrayList.add(stu);
        }

    // 查看学生方法
    public Student[] findAllStudent() {
        Student[] student = new Student[studentArrayList.size()];

        for (int i = 0; i < student.length; i++) {
            student[i] = studentArrayList.get(i);
        }
        return student;
    }

    public void deleteStudentById(String delId) {
        // 1. 查找id在容器中所在的索引位置
        int index = getIndex(delId);
        // 2. 将该索引位置,使用null元素进行覆盖
        studentArrayList.remove(delId);
    }

    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < studentArrayList.size(); i++) {
            Student stu = studentArrayList.get(i);
            if( studentArrayList.get(i).getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {
        // 1. 查找updateId, 在容器中的索引位置
        int index = getIndex(updateId);
        // 2. 将该索引位置, 使用新的学生对象替换
        studentArrayList.set(index,newStu);
    }
}